﻿namespace CommLib
{
    public class MessageSmsList :BluetoothMessage
    {
        public MessageSmsList(int threadId, Sms[] s)
		    : base(BtMessageType.SmsList, null)
        {
		    mData = new string[1 + s.Length * Sms.FIELD_COUNT];

		    int i = 0;
		    mData[i++] = threadId.ToString();

		    foreach (Sms sms in s) {
			    mData[i++] = sms.getText();
			    mData[i++] = sms.getDate().Ticks.ToString();
			    mData[i++] = ((int)sms.getType()).ToString();
			    mData[i++] = sms.getThreadId().ToString();
			    mData[i++] = sms.getNumber();
			    mData[i++] = ((int)sms.getStatus()).ToString();
		    }
	    }

        public MessageSmsList(byte[] data, int length)
            : base(BtMessageType.SmsList, data, length)
        {
        }

        public int getThreadId()
        {
            return int.Parse(mData[0]);
        }

        public Sms[] getSmsList()
        {
            Sms[] list = null;

            if ((mData.Length - 1) % Sms.FIELD_COUNT == 0)
            {
                list = new Sms[(mData.Length - 1) / Sms.FIELD_COUNT];

                int idx = 0;
                for (int i = 1; i < mData.Length; i += Sms.FIELD_COUNT)
                {
                    list[idx++] = new Sms(mData, i);
                }
            }

            return list;
        }
    }
}
